<?php
class Pagination{
	private $rowCount;
	private $limit;
	private $start;
	private $pages;
	private $initPage;
	private $link;
	private $paging;
	
	public function __construct($link,$rowCount=0,$start=0,$limit=0){
		$this->setLimit($limit);
		$this->setRowCount($rowCount);
		$this->setStart($start);
		$this->setPages();
		$this->setInitPage();
		$this->setLink($link);
		$this->setLink($this->getLinkQuery("limit",$this->limit));
		$this->setPageing();
	}
	
	public function getLinkQuery($name,$value){
		$query=parse_url  ($this->link);
		$link=$this->link;
		if(isset($query['query']))
			$link.='&'.$name.'='.$value;
		else
			$link.='?'.$name.'='.$value;
		return $link;
	}
	
	public function setLink($link){
		$this->link=$link;
	}
	
	public function setRowCount($count){
		if(is_numeric($count)){
			$this->rowCount=$count;
			return true;
		} 
		return false;
	}
	
	public function setLimit($limit){
		if(is_numeric($limit)){
			$this->limit=$limit;
			return true;
		} 
		
		return false;
	}
	
	public function setStart($start){
		if(is_numeric($start)){
			$this->start=$start;
			return true;
		} 
		return false;
	}
	
	private function setPages(){
		$pages=0;
		if($this->limit>0)
			$pages=ceil($this->rowCount/$this->limit);
		$this->pages=$pages;
	}
	
	private function setInitPage(){
		$init=1;
		if($this->limit>0){
			$init=(ceil($this->start/$this->limit))+1;
		}
		$this->initPage=$init;
	}
	
	public function displayPageLinks(){
		$html = '';
		if ($this->rowCount) {
			$html="<div class='pages'><ul>";
			if($this->initPage!=1){
				$html.="<li>".$this->getPageStartDisplay()."</li>";
			}
			foreach($this->paging as $key=>$value){
				$link=$this->getLinkQuery("start",$value);
				if($key!=$this->initPage) {
					$html.='<li><a href="'.$link.'">'.$key.'</a></li>';
				} else {
					$html.='<li class="active">'.$key.'</li>';
				}
			}
			if($this->initPage!=$this->pages && $this->rowCount>0 &&$this->pages>1){
				$html.=$this->getPageEndDisplay();
			}
			$html .= "</ul></div>";
		}
		return $html;
	}
	
	private function setPageing(){
		$start=ceil($this->initPage/10);
		if($start>1)
			$start=(($start-1)*10)+1;
		$end=$start+10;
		if($end>$this->pages)
			$end=$this->pages+1;
		$paging=array();
		for($i=$start;$i<$end;$i++){
			$paging[$i]=($i-1)*$this->limit;
		}
		$this->paging=$paging;
	}
	
	private function getPageEndDisplay(){
		$html="";
		$link=$this->getLinkQuery("start",(($this->initPage*$this->limit)));
		$html.='<li><a href="'.$link.'">next</a></li>';
		$link=$this->getLinkQuery("start",($this->pages-1)*$this->limit);
		$html.='<li><a href="'.$link.'">end</a></li>';
		return $html;
	}
	
	private function getPageStartDisplay(){
		$html="";
		$link=$this->getLinkQuery("start",0);
		$html.='<li><a href="'.$link.'">start</a></li>';
		$link=$this->getLinkQuery("start",((($this->initPage-2)*$this->limit)));
		$html.='<li><a href="'.$link.'">previous</a></li>';
		return $html;
	}
	
	public function displayPageCount(){
		$html="";
		if($this->rowCount>0)
			$html.="<div class='pageCount'><p>page ".$this->initPage." of ".$this->pages."</p></div>";
		return $html;
	}
	
	public function displaySelectLimit(){
		$html="";
		if($this->rowCount<=0)
			return "";
		$array=array("5","10","15","20","25","30","50","100","all");
		//$link=$this->getLinkQuery("start",REQUEST::getVar('start',0));
		$html="<div class='selectLimit'><form action='".$this->link."' method='post'><label for='limit'>Results per page: </label>";
		$html.="<select id='limit' name='limit' onchange='submit();'>";
		foreach($array as $key=>$value){
			$html.="<option value='$value' ";
			if(REQUEST::getVar('limit',20)==$value)
				$html.="selected='selected'";
			$html.=">";
			$html.=$value;
			$html.="</option>";
		}
	    $html.="</select>";
	    $html.="</form></div>";
	    return $html;
	}
	
	public function displayPagingFooter(){
		$html = '';
		if ($this->rowCount)
		$html="<div class='pagination'>";
		$html.=$this->displaySelectLimit();
		$html.=$this->displayPageLinks();
		$html.=$this->displayPageCount();
		if ($this->rowCount)
		$html.="</div>";
		return $html;
	}
}
?>